package com.ywgt.collection;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArraySet;

public class SetSafe {
    public static void main(String[] args) {
       // Set<String> set=new HashSet<>();
       // 解决方案1：Set<String> set= Collections.synchronizedSet(new HashSet<String>());
        //解决方案2
        Set<String> set=new CopyOnWriteArraySet<>();
        for (int i=0;i<20;i++){
            new Thread(()->{
                set.add(UUID.randomUUID().toString().substring(10));
                System.out.println(set);
            }
            ,String.valueOf(i)).start();

        }
    }
}
